<template>
 <transition name="fade">
  <div class="my-dialog__wrapper"  v-if="visible" @click.self="$emit('update:visible', !visible)">
    <div :style="{ 'margin-top': `${top}px`, width: width }" class="my-dialog">
      <!-- 头部 -->
      <div class="my-dialog__header">
        <span class="my-dialog__title">
          <slot name="title">{{ title }}</slot>
        </span>
        <!-- 关闭按钮 -->
        <!-- 就是点击这个字体图标关闭这个对话框-->
        <button
          v-if="showClose"
          type="button"
          aria-label="Close"
          class="my-dialog__headerbtn"
          @click="$emit('update:visible', !visible)"
        >
          <i class="iconfont icon-close"></i>
        </button>
      </div>
      <!-- 主体 -->
      <div class="my-dialog__body">
        <slot></slot>
      </div>
      <!-- 脚部 -->
      <div class="my-dialog__footer">
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</transition>
</template>
<script>
export default {
  name: 'my-dialog',
  props: {
    // 是否显示关闭按钮
    'show-close': {
      type: Boolean,
      default: true
    },
    // 距离顶部间隙
    top: {
      type: String,
      default: '0'
    },
    // 宽度
    width: {
      type: String,
      default: '50%'
    },
    // 标题
    title: {
      type: String,
      default: '标题'
    },
    // v-model使用
    value: {
      type: Boolean,
      default: false
    },
    // sync使用的
    visible: {
      type: Boolean,
      default: false
    }
  }
}
</script>

<style lang="less">
.my-dialog__wrapper {
  z-index: 2011;
  margin: 0;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
  margin: 0;
  // 半透明的背景
  background-color: #80808091;
  .my-dialog {
    position: relative;
    margin: 0 auto 50px;
    background: #fff;
    border-radius: 2px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    box-sizing: border-box;
    width: 50%;
    .my-dialog__header {
      padding: 20px 20px 10px;
      .my-dialog__title {
        line-height: 24px;
        font-size: 18px;
        color: #303133;
      }
      .my-dialog__headerbtn {
        position: absolute;
        top: 20px;
        right: 20px;
        padding: 0;
        background: transparent;
        border: none;
        outline: none;
        cursor: pointer;
        font-size: 16px;
        .iconfont {
          color: #909399;
        }
      }
    }
    .my-dialog__body {
      padding: 30px 20px;
      color: #606266;
      font-size: 14px;
      word-break: break-all;
    }
    .my-dialog__footer {
      padding: 10px 20px 20px;
      text-align: right;
      box-sizing: border-box;
    }
  }
}
.fade-enter-active, .fade-leave-active {
  transition: opacity 1s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
</style>
